
//上传  点击上传文件,触发file的上传文件

//委托给body

$('body').on('click', '#uploadSwiper', function () {
  $('#myfile').trigger('click')
})

//获取数据
var form = layui.form
function gain() {
  $.ajax({
    type: 'get',
    url: '/admin/swipers',
    success: function (res) {
      console.log(res);
      var tags = template('table-tpl', res)
      // console.log(tags);
      $('.layui-table tbody').html(tags)
    }
  })
}


gain()

// 上传
$('body').on('change', '#myfile', function (e) {
  let files = e.target.files
  let fd = new FormData()
  // console.log(files);

  $.each(files, function (index, item) {
    fd.append('swipers', item)
  })
  $.ajax({
    type: 'post',
    url: '/admin/swipers',
    data: fd,
    processData: false,
    contentType: false,
    success: function (res) {
      if (res.status === 0) {
        layer.msg(res.message)
        gain()
      }
    }
  })
})


// 轮播图状态切换
$('body').on('click', '.layui-badge', function (e) {
  let status = $(e.target).data('status')
  let id = $(e.target).data('id')
  $.ajax({
    type: 'put',
    url: '/admin/swipers/' + id,
    data: {
      status: status
    },
    success: function (res) {
      if (res.status === 0) {
        // 切换成功
        layer.msg(res.message)
        gain()
      }
    }
  })
})

//删除

$('body').on('click', '.delete', function (e) {
  let id = $(e.target).data('id')
  console.log(id);//获取的是图片的id值
  layer.confirm('确定删除吗', function (index) {
    $.ajax({
      type: 'DELETE',
      url: '/admin/swipers/' + id,
      success: function (res) {
        if (res.status === 0) {
          // 删除成功
          layer.close(index)
          gain()
        }
      }
    })
  })
})


